package com.zhangtai.aspect;

import com.zhangtai.exception.SendErr;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
@Aspect
@Slf4j
public class TimeAspect {
//    @Pointcut("execution(* com.zhangtai.modules.controller.admin.*.*(..))")  //com.zhangtai.modules.controller
//    public void pointCut(){
//    }
    @Autowired
    private SendErr sendErr;

    @Pointcut("execution(* com.zhangtai.modules.controller.admin.EmployessController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut1(){
    }

//    @Pointcut("execution(* com.zhangtai.modules.controller.admin.CommonController.*(..))")  //com.zhangtai.modules.controller
//    public void pointCut3(){
//    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.DimissionController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut4(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.EmployerCompanyController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut5(){
    }
//    @Pointcut("execution(* com.zhangtai.modules.controller.admin.EsignController.*(..))")  //com.zhangtai.modules.controller
//    public void pointCut6(){
//    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.IndexInfoController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut7(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.LeaveController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut8(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.MemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut9(){
    }

    @Pointcut("execution(* com.zhangtai.modules.controller.admin.NoticeController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut10(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.PromotionController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut11(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.RegularController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut12(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.SalaryController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut13(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.SecurityController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut14(){
    }

    @Pointcut("execution(* com.zhangtai.modules.controller.admin.TransferController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut15(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.UserController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut16(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.admin.UserRoleController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut17(){
    }

    @Pointcut("execution(* com.zhangtai.modules.controller.member.ContractMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut18(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.DimissionMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut19(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.HomeMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut20(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.LeaveMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut21(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.MemberRegularController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut22(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.PromotionMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut23(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.SalaryMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut24(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.SecurityMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut25(){
    }
    @Pointcut("execution(* com.zhangtai.modules.controller.member.SelfHelpMemberController.*(..))")  //com.zhangtai.modules.controller
    public void pointCut26(){
    }

//
    @Around("pointCut1()||pointCut4()||pointCut5()||pointCut7()||pointCut8()" +
            "||pointCut9()||pointCut10()||pointCut11()||pointCut12()||pointCut13()||pointCut14()||pointCut15()||pointCut16()||pointCut17()" +
            "||pointCut19()||pointCut20()||pointCut21()||pointCut22()||pointCut23()||pointCut24()||pointCut25()||pointCut26()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();






        long l = System.currentTimeMillis();
        Object proceed = proceedingJoinPoint.proceed();
        long l1 = System.currentTimeMillis();
        Long time =l1-l;
        if (!ObjectUtils.isEmpty(attributes)){
            HttpServletRequest request = attributes.getRequest();
            Object[] args = proceedingJoinPoint.getArgs();
            List<Object> objects = Arrays.asList(args);
            ArrayList<Object> arrayList = new ArrayList<>();

            for (Object object : objects) {
                if (object!=null){
                    arrayList.add(object);
                }
            }

            if (time>1200){
                sendErr.sendMsg(request.getRequestURL()+"耗时:"+time+"ms");
                log.error("{},参数： 耗时=============>>>>>>:{} ms",request.getRequestURL(),time);
            }

        }else {

            if (time>1200){
                sendErr.sendMsg(proceedingJoinPoint.getSignature()+"耗时:"+time+"ms");
                log.error("{},耗时=============>>>>>>:{} ms",proceedingJoinPoint.getSignature(),time);
            }

        }

        return proceed;

    }


    }
